package br.com.marcelo.reservasis.client.util;

import br.com.aroeirasoftware.gwtcomponents.client.components.ASComboBox;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQuery;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQueryObject;
import br.com.aroeirasoftware.gwtcomponents.client.query.ASQueryResult;
import br.com.marcelo.reservasis.client.services.apartamento.ApartamentoUIService;
import br.com.marcelo.reservasis.client.services.reservaestadia.ReservaEstadiaUIService;
import br.com.marcelo.reservasis.domain.model.Apartamento;
import br.com.marcelo.reservasis.domain.model.TipoApartamento;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 *
 * @author Marcelo
 */
public class ComboBoxUtil {

//    public static void preencherComboBoxEstados(final List<Estado> lista, final ILCampoHorizontal campo) {
//        ILComboBox<Estado> comboBox = (ILComboBox<Estado>) campo.getComponente();
//
//        for (Estado e : lista) {
//            comboBox.addItem(e.getSigla(), e.getId().toString(), e);
//        }
//    }
//
    public static void preencherComboBoxTipoApartamento(final ASComboBox<TipoApartamento> combo) {

        ASQueryObject qo = new ASQueryObject(TipoApartamento.class);
        ASQuery consulta = qo.getConsulta(false);
        ApartamentoUIService.Async.getInstance().getTipoApartamentoPorConsulta(consulta, new AsyncCallback<ASQueryResult<TipoApartamento>>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(ASQueryResult<TipoApartamento> result) {
                if (result != null && result.getLista() != null) {
                    for (TipoApartamento ta : result.getLista()) {
                        combo.addObjeto(ta, ta.getDescricao());
                    }
                }
            }
        });
    }

    public static void preencherComboBoxApartamento(final ASComboBox<Apartamento> combo, TipoApartamento tp) {


        ASQueryObject qo = new ASQueryObject(Apartamento.class, "ap");
        qo.addSelect(ASQueryObject.Expressions.as("ap.idapart", "idapart"));
        qo.addSelect(ASQueryObject.Expressions.as("ap.descricaoApart", "descricaoApart"));
        qo.addSelect(ASQueryObject.Expressions.as("ap.numApart", "numApart"));
        qo.addSelect(ASQueryObject.Expressions.as("ap.numQuartosApart", "numQuartosApart"));
        qo.addSelect(ASQueryObject.Expressions.as("ap.tipoApartamento", "tipoApartamento"));
        qo.addWhere(ASQueryObject.Expressions.eq("ap.tipoApartamento", tp.getIdtipo()));

        ASQuery consulta = qo.getConsulta(true);
        ReservaEstadiaUIService.Async.getInstance().getApartamentoPorConsulta(consulta, new AsyncCallback<ASQueryResult<Apartamento>>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(ASQueryResult<Apartamento> result) {
                if (result != null && result.getLista() != null) {
                    for (Apartamento ap : result.getLista()) {
                        combo.addObjeto(ap, ap.getDescricaoApart());
                    }
                }
            }
        });
    }
}
